﻿using System;
using System.IO;

namespace ImageProcessing.Comm
{
    public class DirectoryCleaner
    {
        public static void ClearDirectoryContents(string directoryPath)
        {
            // 检查目录是否存在
            if (!Directory.Exists(directoryPath))
            {
                throw new DirectoryNotFoundException($"The directory '{directoryPath}' does not exist.");
            }

            // 获取目录中的所有文件和子目录
            string[] files = Directory.GetFiles(directoryPath);
            string[] directories = Directory.GetDirectories(directoryPath);

            // 删除文件
            foreach (string file in files)
            {
                File.Delete(file);
            }

            // 删除子目录及其内容
            foreach (string dir in directories)
            {
                DeleteDirectory(dir);
            }
        }

        private static void DeleteDirectory(string directoryPath)
        {
            // 递归删除子目录
            string[] subDirectories = Directory.GetDirectories(directoryPath);
            foreach (string dir in subDirectories)
            {
                DeleteDirectory(dir);
            }

            // 删除目录
            Directory.Delete(directoryPath, false);
        }
    }
}